package com.linktrip.devicemanager.control.sw;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.google.gson.Gson;
import com.linktrip.devicemanager.entity.Upgrade;
import com.linktrip.devicemanager.server.UpgradeService;
import com.linktrip.devicemanager.util.Constant;
import com.linktrip.devicemanager.util.FileUtil;
import com.linktrip.devicemanager.util.PageInfo;
import com.linktrip.devicemanager.util.ToolKits;

@Controller
@RequestMapping(value = "/service/upgrade")
@ResponseBody
public class UpgradeControl {
	Logger logger = LoggerFactory.getLogger(UpgradeControl.class);
	@Autowired
	UpgradeService server;

	@RequestMapping(value = "/addSW.lt", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String addUpgradeSW(@RequestParam MultipartFile uFile, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String swName = ToolKits.getRequestParamValue(request, "swName");
		int swVersion = ToolKits.getRequestParamValueForInt(request, "swVersion");
		int swHwVersion = ToolKits.getRequestParamValueForInt(request, "swHwVersion");
		int swIdStart = ToolKits.getRequestParamValueForInt(request, "swIdStart");
		int swIdEnd = ToolKits.getRequestParamValueForInt(request, "swIdEnd");
		String remark = ToolKits.getRequestParamValue(request, "remark");
		boolean result = false;
		if (ToolKits.hasValue(swHwVersion, swHwVersion, swIdEnd, swIdStart)) {
			String target = request.getSession().getServletContext().getRealPath("/WEB-INF") + "/upload/" + swHwVersion
					+ "/" + swVersion + "/";
			target = target.replaceAll("\\\\", "/");
			String prefix = swName;
			Integer total = FileUtil.splitFile(uFile.getBytes(), target, prefix);
			if (total > 0) {
				Upgrade entity = new Upgrade();
				entity.setSwName(prefix);
				entity.setTargetPath(target);
				entity.setSwVersion(swVersion);
				entity.setSwHwVersion(swHwVersion);
				entity.setSwIdStart(swIdStart);
				entity.setSwIdEnd(swIdEnd);
				entity.setTotal(total);
				entity.setCreate_date(System.currentTimeMillis());
				entity.setRemark(remark);
				logger.info("SW entity:{}", entity.toString());
				result = server.save(entity);
			}
		}
		int res = result ? Constant.RESPONSE_OK : Constant.RESPONSE_ERR;
		Gson gson = new Gson();
		return gson.toJson(res);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/listSW.lt", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String listSW(HttpServletRequest request, HttpServletResponse response) {

		int pageNo = ToolKits.getRequestParamValueForInt(request, "page_no");
		int pageSize = ToolKits.getRequestParamValueForInt(request, "page_size");

		if (!ToolKits.hasValue(pageNo))
			pageNo = 1;
		if (!ToolKits.hasValue(pageSize))
			pageSize = Constant.PAGE_SIZE;

		int start = (pageNo - 1) * pageSize;
		int end = pageSize;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("end", end);
		List<Upgrade> list = server.getList(map);
		PageInfo<List> plist = new PageInfo<List>(server.count(map), list);
		plist.setPageNo(pageNo);
		plist.setPageSize(pageSize);
		plist.setPagesSize(plist.pagesSize());

		Gson gson = new Gson();
		return gson.toJson(plist);
	}

	@RequestMapping(value = "/updateSW.lt", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String updateSW(HttpServletRequest request, HttpServletResponse response) {

		Integer id = ToolKits.getRequestParamValueForInt(request, "id");
		int swIdStart = ToolKits.getRequestParamValueForInt(request, "swIdStart");
		int swIdEnd = ToolKits.getRequestParamValueForInt(request, "swIdEnd");
		Integer enable = ToolKits.getRequestParamValueForInt(request, "enable");
		String remark = ToolKits.getRequestParamValue(request, "remark");

		boolean result = false;

		if (ToolKits.hasValue(id, swIdEnd, swIdStart, enable)) {
			Upgrade entity = new Upgrade();
			entity.setId(id);
			entity.setSwIdStart(swIdStart);
			entity.setSwIdEnd(swIdEnd);
			entity.setEnable(enable);
			entity.setRemark(remark);
			result = server.update(entity);
		}
		int res = result ? Constant.RESPONSE_OK : Constant.RESPONSE_ERR;
		Gson gson = new Gson();
		return gson.toJson(res);
	}

	@RequestMapping(value = "/deleteSW.lt", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String deleteSW(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response) {

		boolean result = false;
		if (ToolKits.hasValue(id)) {
			Upgrade entity = server.findById(id);
			if (null != entity) {
				result = server.delete(id);
				FileUtil.deleteFile(entity.getTargetPath());
			}
		}
		int res = result ? Constant.RESPONSE_OK : Constant.RESPONSE_ERR;
		Gson gson = new Gson();
		return gson.toJson(res);
	}

}
